package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SalaryReduction;
import com.ruoyi.system.mapper.SalaryStandardMapper;
import com.ruoyi.system.domain.SalaryStandard;
import com.ruoyi.system.service.ISalaryStandardService;

/**
 * 薪资标准Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@Service
public class SalaryStandardServiceImpl implements ISalaryStandardService 
{
    @Autowired
    private SalaryStandardMapper salaryStandardMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Autowired
    private SysDeptMapper sysDeptMapper;
//    @Autowired
//    private SalaryReductionRelatedMapper salaryReductionRelatedMapper;

    /**
     * 查询薪资标准
     * 
     * @param id 薪资标准主键
     * @return 薪资标准
     */
    @Override
    public SalaryStandard selectSalaryStandardById(Long id)
    {
        return salaryStandardMapper.selectSalaryStandardById(id);
    }

    /**
     * 查询薪资标准列表
     * 
     * @param salaryStandard 薪资标准
     * @return 薪资标准
     */
    @Override
    public List<SalaryStandard> selectSalaryStandardList(SalaryStandard salaryStandard)
    {
        List<SalaryStandard> salaryStandardList = salaryStandardMapper.selectSalaryStandardList(salaryStandard);
        for(SalaryStandard ss : salaryStandardList){
            List<SalaryReduction> salaryReductions = salaryStandardMapper.selectSalaryReductionListBySalaryStandardId(ss.getId());
            ss.setSalaryReductionList(salaryReductions);
        }
        return salaryStandardList;
    }

    /**
     * 新增薪资标准
     * 
     * @param salaryStandard 薪资标准
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSalaryStandard(SalaryStandard salaryStandard)
    {
        salaryStandard.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser = sysUserMapper.selectUserById(salaryStandard.getUserId());
        SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
        salaryStandard.setDeptName(sysDept.getDeptName());
        salaryStandard.setUserName(sysUser.getUserName());
        int rows = salaryStandardMapper.insertSalaryStandard(salaryStandard);
        Long id = Long.valueOf(rows);
        //将rows转化成Long类型



        //获取到扣款list
        List<SalaryReduction> salaryReductionList = salaryStandard.getSalaryReductionList();
        List<SalaryReduction> list = new ArrayList<>();
        for(SalaryReduction salaryReduction : salaryReductionList){
            SalaryReduction sr = new SalaryReduction(salaryStandard.getId(),salaryReduction.getName(),salaryReduction.getMoney());
            list.add(sr);

        }

        insertSalaryReduction(list);



        return rows;
    }

    /**
     * 修改薪资标准
     * 
     * @param salaryStandard 薪资标准
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSalaryStandard(SalaryStandard salaryStandard)
    {

        salaryStandard.setUpdateTime(DateUtils.getNowDate());

        salaryStandardMapper.deleteSalaryReductionByStandardId(salaryStandard.getId());


        List<SalaryReduction> salaryReductionList = salaryStandard.getSalaryReductionList();
        List<SalaryReduction> list = new ArrayList<>();
        for(SalaryReduction salaryReduction : salaryReductionList){
            SalaryReduction sr = new SalaryReduction(salaryStandard.getId(),salaryReduction.getName(),salaryReduction.getMoney());
            list.add(sr);
        }
        insertSalaryReduction(list);
        return salaryStandardMapper.updateSalaryStandard(salaryStandard);
    }

    /**
     * 批量删除薪资标准
     * 
     * @param ids 需要删除的薪资标准主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalaryStandardByIds(Long[] ids)
    {
        salaryStandardMapper.deleteSalaryReductionByIds(ids);
        return salaryStandardMapper.deleteSalaryStandardByIds(ids);
    }

    /**
     * 删除薪资标准信息
     * 
     * @param id 薪资标准主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalaryStandardById(Long id)
    {
        salaryStandardMapper.deleteSalaryReductionById(id);
        return salaryStandardMapper.deleteSalaryStandardById(id);
    }

    @Override
    public List<SalaryReduction> selectSalaryReductionListBySalaryStandardId(Long id) {
        List<SalaryReduction> list = salaryStandardMapper.selectSalaryReductionListBySalaryStandardId(id);
        return list;
    }

    /**
     * 新增扣款管理信息
     * 
     * @param salaryReductionList 薪资标准对象
     */
    public void insertSalaryReduction(List<SalaryReduction> salaryReductionList)
    {
//        List<SalaryReduction> salaryReductionList = salaryStandard.getSalaryReductionList();
//        Long id = salaryStandard.getId();
//        if (StringUtils.isNotNull(salaryReductionList))
//        {
//            List<SalaryReduction> list = new ArrayList<SalaryReduction>();
//            for (SalaryReduction salaryReduction : salaryReductionList)
//            {
//                salaryReduction.setId(id);
//                list.add(salaryReduction);
//            }
//            if (list.size() > 0)
//            {
//                salaryStandardMapper.batchSalaryReduction(list);
//            }
//        }


        if (StringUtils.isNotNull(salaryReductionList))
        {
            salaryStandardMapper.batchSalaryReduction(salaryReductionList);

        }
    }
}
